<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Settings extends Controller
{
	private $_template_f;
	private $_lang;
	
	private $_template_path = '.././app/config/config_template.php';
    private $_config_path = '.././app/config/config_custom.php';
    private $_fTempUpload = '../images/uploads/';
    
	function __construct()
	{
		parent::Controller();
		$this->_lang = $this->mycommon->getLangCode();
		$this->_template_f = $this->config->item('template_f');
		$this->load->language('common', $this->_lang);
		$common_lang = $this->lang->line('common_lang');
		if(!$this->session->userdata('uname'))
		{
			redirect(site_url('/login'));
			die();
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > 0)
			{
				show_error($common_lang['access_denied']);
				die();
			}
		}
		
		$this->load->model('settings/settings_model');
		$this->load->language('common', $this->_lang);
		$this->load->language('settings/settings', $this->_lang);
	}
	
	function _loadHeader($title = '')
	{
		$header = array();
		$header['title'] = $title == '' ? 'Self serving' : $title;
		$header['template_f'] = $this->_template_f;
		
		$header['username'] = '';
		$header['userid'] = '';
		$header['permitid'] = '';
		$header['lang'] = $this->_lang;
		$header['module_name'] = 'home';
		if($this->session->userdata('uname'))
		{
			$header['username'] = $this->session->userdata('uname');
			$header['userid'] = $this->session->userdata('uid');
			$header['permitid'] = $this->session->userdata('permitid');
		}
		
		// load header
		$this->load->view($this->_template_f . 'preheader_view');
		$this->load->view($this->_template_f . 'header_view', $header);
	}
	
	function _loadMenu()
	{
		$data = array();
		$data['username'] = '';
		$data['userid'] = '';
		$data['permitid'] = '';
		$data['lang'] = $this->_lang;
		if($this->session->userdata('uname'))
		{
			$data['username'] = $this->session->userdata('uname');
			$data['userid'] = $this->session->userdata('uid');
			$data['permitid'] = $this->session->userdata('permitid');
		}
		$data['common_lang'] = $this->lang->line('common_lang');
		
		$this->load->view($this->_template_f . 'menu_view', $data);
	}
	
	function _loadFooter()
	{
		$this->load->view($this->_template_f . 'footer_view');
	}
	
	function deluploadtemp()
	{
		$data = array();
		$data['lstFile'] = array();
		$dat['lang'] = $this->_lang;
		$data['settings_lang'] = $this->lang->line('settings_lang');
		
		// load header
		$this->_loadHeader($data['settings_lang']['module_title']);
		
		// load menu
		$this->_loadMenu();
		
		$handle = opendir($this->_fTempUpload);
		$currTime = strtotime(date('d-m-Y'));
		$currTime = $currTime - 3600;
		while($file = readdir($handle))
		{
			$filePath = $this->_fTempUpload . $file;
			if(is_file($filePath))
			{
				$changeFileTime = filectime($filePath);
				if($changeFileTime < $currTime)
				{
					$data['lstFile'][] = $filePath;
					unlink($filePath);
				}
			}
		}
		closedir($handle);
		
		// load content
		$this->load->view($this->_template_f . 'settings/settings_deletetempupload_view', $data);
		
		// load footer
		$this->_loadFooter();
	}
	
	function view()
	{
		$data = array();
		$dat['lang'] = $this->_lang;
		$data['settings_lang'] = $this->lang->line('settings_lang');
		
		// load header
		$this->_loadHeader($data['settings_lang']['module_title']);
		
		// load menu
		$this->_loadMenu();
		
		// load content
		$this->load->view($this->_template_f . 'settings/settings_view', $data);
		
		// load footer
		$this->_loadFooter();
	}
		
	function template()
	{
		$data = array();
        $data['mess'] = '';
        $data['fileContent'] = '';
        $dat['lang'] = $this->_lang;
		$data['settings_lang'] = $this->lang->line('settings_lang');
                
        if(isset($_POST['content']))
        {
            $content = $this->input->post('content');
            if($content != '')
            {
                // open file 
                $fw = fopen($this->_template_path, 'w') or die($data['settings_lang']['template_error_open_file']);
                // write to file
                // added stripslashes to $newdata
                $fb = fwrite($fw, stripslashes($content)) or die($data['settings_lang']['template_error_read_file']);
                // close file
                fclose($fw);
                $data['mess'] = $data['settings_lang']['template_update_ok'];
            }
            else
            {
                $data['mess'] = $data['settings_lang']['template_error_invalid_content'];
            }
            
        }
        
        // open file
        $fh = fopen($this->_template_path, "r") or die($data['settings_lang']['template_error_open_file']);
        // read file contents
        $fileContent = fread($fh, filesize($this->_template_path)) or die($data['settings_lang']['template_error_read_file']);
        // close file
        fclose($fh);
        $data['fileContent'] = $fileContent;
        
        
        // load header
        $this->_loadHeader($data['settings_lang']['module_title'] . ' - ' . $data['settings_lang']['setting_template']);
        
        // load menu
        $this->_loadMenu();
        
        // load content
        $this->load->view($this->_template_f . 'settings/settings_template_view', $data);
        
        // load footer
        $this->_loadFooter();
	}
	
	function config()
	{
		$data = array();
        $data['mess'] = '';
        $dat['lang'] = $this->_lang;
		$data['settings_lang'] = $this->lang->line('settings_lang');
 
        if(isset($_POST['cprice']))
        {
            $cpc_price = $this->mycommon->getSaveSqlStr($this->input->post('cprice'));
            $cpm_price = $this->mycommon->getSaveSqlStr($this->input->post('cpm_price'));
            $admin_email = $this->mycommon->getSaveSqlStr($this->input->post('admin_email', true));
            $publisher_email = $this->mycommon->getSaveSqlStr($this->input->post('publisher_email', true));
            $max_title = $this->mycommon->getSaveSqlStr($this->input->post('max_title'));
            $max_body_text = $this->mycommon->getSaveSqlStr($this->input->post('max_body_text'));
            $image_w = $this->mycommon->getSaveSqlStr($this->input->post('image_w'));
            $image_h = $this->mycommon->getSaveSqlStr($this->input->post('image_h'));
            $serving_email = $this->mycommon->getSaveSqlStr($this->input->post('serving_email'));
            $serving_email_name = $this->mycommon->getSaveSqlStr($this->input->post('serving_email_name', true));
            
            $money_unit_value = $this->mycommon->getSaveSqlStr($this->input->post('money_unit_value', true));
            $max_title_set_style = $this->mycommon->getSaveSqlStr($this->input->post('max_title_set_style', true));
            $max_body_set_style = $this->mycommon->getSaveSqlStr($this->input->post('max_body_set_style', true));
            
            $show_custom_error = $this->mycommon->getSaveSqlStr($this->input->post('show_custom_error'));
            
            
            // update to database
            $this->settings_model->settings_update($cpc_price, $cpm_price, $admin_email, $publisher_email, $max_title, $max_body_text, $image_w, $image_h, $serving_email, $serving_email_name, $money_unit_value, $max_title_set_style, $max_body_set_style, $show_custom_error);
            
            
            $fileContent = "<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n\n";
            $fileContent .= "\t\$config['cpc_price'] = " . $cpc_price . ";\n\n";
            $fileContent .= "\t\$config['cpm_price'] = " . $cpm_price . ";\n\n";
            $fileContent .= "\t\$config['admin_email'] = array(" . $admin_email . ");\n\n";
            $fileContent .= "\t\$config['publisher_email'] = array(" . $publisher_email . ");\n\n";
            $fileContent .= "\t\$config['serving_email'] = '" . $serving_email . "';\n\n";
            $fileContent .= "\t\$config['serving_email_name'] = array(" . $serving_email_name . ");\n\n";
            $fileContent .= "\t\$config['ad_max_title'] = " . $max_title . ";\n\n";
            $fileContent .= "\t\$config['ad_max_body_text'] = " . $max_body_text . ";\n\n";
            $fileContent .= "\t\$config['ad_image_w'] = " . $image_w . ";\n\n";
            $fileContent .= "\t\$config['ad_image_h'] = " . $image_h . ";\n\n";
            $fileContent .= "\t\$config['money_unit_value'] = " . $money_unit_value . ";\n\n";
            $fileContent .= "\t\$config['max_title_set_style'] = " . $max_title_set_style . ";\n\n";
            $fileContent .= "\t\$config['max_body_set_style'] = " . $max_body_set_style . ";\n\n";
            $fileContent .= "\t\$config['show_custom_error'] = " . $show_custom_error . ";\n\n";
            
            // open file 
            $fw = fopen($this->_config_path, 'w') or die($data['settings_lang']['template_error_open_file']);
            // write to file
            // added stripslashes to $newdata
            $fb = fwrite($fw, stripslashes($fileContent)) or die($data['settings_lang']['template_error_read_file']);
            // close file
            fclose($fw);
            $data['mess'] = $data['settings_lang']['template_update_ok'];
        }
        
      
        
        // load header
        $this->_loadHeader($data['settings_lang']['module_title'] . ' - ' . $data['settings_lang']['setting_config']);
        
        // load menu
        $this->_loadMenu();
        
        // load content
        $data['config'] = $this->settings_model->get_all();
        
				
		$this->load->view($this->_template_f . 'settings/settings_config_view', $data);
        
        // load footer
        $this->_loadFooter();
	}
	
	function set_all_channel()
	{
		$action = trim($this->input->post('action'));
		
		if($action == 1)
		{
			$gCatChannel = $this->settings_model->group_category_get_all();
			
			$strSiteName = '';
			$strChannelId = '';
			
			foreach($gCatChannel as $key=>$priceItem)
			{
				foreach($priceItem as $gcat)
				{
					foreach($gcat['lstSite'] as $site)
					{
						$strSiteName .= ($strSiteName != '') ? ', ' . $site['channelname'] : $site['channelname'];
						foreach($site['lstChannel'] as $channel)
						{
							$strChannelId .= ($strChannelId != '') ? ',' . $channel['channelid'] : $channel['channelid'];
						}
					}
				}
			}
			
			$uid = $this->session->userdata('uid');
			$gCatChannel = $this->settings_model->group_category_get_all();
			
			mysqli_query($this->db->conn_id, "CALL selfserving_admin_set_all_site_channel('$uid', '', '$strChannelId', '$strSiteName');") or die(mysqli_error($this->db->conn_id));
			die ('ok');
		}
		else if($action == 2)
		{
			$lstId = $this->input->post('id');
			$arrId = explode(',', $lstId);
			$strId = '';
			foreach($arrId as $id)
			{
				if(is_numeric($id) && $id > 0)
				{
					$strId .= ($strId != '') ? ',' . $id : $id;
				}
			}
			$gCatChannel = $this->settings_model->group_category_get_all();
			
			$strSiteName = '';
			$strChannelId = '';
			
			foreach($gCatChannel as $key=>$priceItem)
			{
				foreach($priceItem as $gcat)
				{
					foreach($gcat['lstSite'] as $site)
					{
						$strSiteName .= ($strSiteName != '') ? ', ' . $site['channelname'] : $site['channelname'];
						foreach($site['lstChannel'] as $channel)
						{
							$strChannelId .= ($strChannelId != '') ? ',' . $channel['channelid'] : $channel['channelid'];
						}
					}
				}
			}
			$uid = $this->session->userdata('uid');
			$gCatChannel = $this->settings_model->group_category_get_all();
			
			mysqli_query($this->db->conn_id, "CALL selfserving_admin_set_all_site_channel('$uid', '$strId', '$strChannelId', '$strSiteName');") or die(mysqli_error($this->db->conn_id));
			die('ok');
		}
		else
		{
			$data = array();
			$dat['lang'] = $this->_lang;
			$data['settings_lang'] = $this->lang->line('settings_lang');
			
			// load header
			$this->_loadHeader($data['settings_lang']['module_title']);
			
			// load menu
			$this->_loadMenu();
			
			
			$gCatChannel = $this->settings_model->group_category_get_all();
			
			$strSiteName = '';
			$strChannelId = '';
			
			foreach($gCatChannel as $key=>$priceItem)
			{
				foreach($priceItem as $gcat)
				{
					foreach($gcat['lstSite'] as $site)
					{
						$strSiteName .= ($strSiteName != '') ? ', ' . $site['channelname'] : $site['channelname'];
						foreach($site['lstChannel'] as $channel)
						{
							$strChannelId .= ($strChannelId != '') ? ',' . $channel['channelid'] : $channel['channelid'];
						}
					}
				}
			}
			$arr = explode(',',$strChannelId);
			echo count($arr);
			
			
			
			// load content
			$this->load->view($this->_template_f . 'settings/set_all_channel_view', $data);
			
			// load footer
			$this->_loadFooter();
		}
	}

	function set_config_site_channel()
	{
		$set = trim($this->input->get('set'));
		if($set == 1)
		{
			$info = $this->settings_model->group_category_get_all();
			$sql = "UPDATE selfserving_setting SET `CPC_lstallchannel` = '" . $info['strChannelId'] . "', `CPC_lstallsite` = '" . $info['strSiteName'] . "';";
			$this->db->query($sql);
			die('ok');
		}
		else
		{
			$data = array();
			$dat['lang'] = $this->_lang;
			$data['settings_lang'] = $this->lang->line('settings_lang');
			
			// load header
			$this->_loadHeader($data['settings_lang']['module_title']);
			
			// load menu
			$this->_loadMenu();
			
			// load content
			$data['info'] = $this->settings_model->get_all();
			$data['numChannelId'] = 0;
			if($data['info']['CPC_lstallchannel'] != '')
			{
				$arr = explode(',', $data['info']['CPC_lstallchannel']);
				$data['numChannelId'] = count($arr);
				$arr = null;
			}
			$data['numSite'] = 0;
			if($data['info']['CPC_lstallsite'] != '')
			{
				$arr = explode(',', $data['info']['CPC_lstallsite']);
				$data['numSite'] = count($arr);
				$arr = null;
			}
			
			$this->load->view($this->_template_f . 'settings/set_config_site_channel_view', $data);
			
			// load footer
			$this->_loadFooter();
		}
	}
	
}